/*
 * @Author: your name
 * @Date: 2021-03-08 09:28:47
 * @LastEditTime: 2021-03-09 19:23:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \day\src\models\students.ts
 */
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {students,studentID} from '@/services/stydents'
import {IStudents} from '@/interface/students'
export interface StudentModelState {
   Students:IStudents[],
   student_id:string
  }
  
  export interface StudentModelType {
    namespace: 'students';
    state: StudentModelState;
    effects: {
        getStudents: Effect;
        DelStudent:Effect
    };
    reducers: {
      save: Reducer<StudentModelState>;
      // 启用 immer 之后
      // save: ImmerReducer<IndexModelState>;
    };
    subscriptions: { setup: Subscription};
  }
  
  const StudentModel: StudentModelType = {
    namespace: 'students',
    state: {
      Students:[],
      student_id:''
    },
    effects: {
      *getStudents({ payload }, { call, put }) {
        let result = yield call(students);
        if(result.code === 1){
          console.log(result);
            yield put({
                type: 'save',
                payload:{Students:result.data}
            })
        }
      },
      *DelStudent({ payload }, { call, put }) {
        let res = yield call(studentID, payload)
        if (res.code === 1) {
            yield put({
                type: 'save',
            })
            yield put({
                type: 'getStudents'
            })
        }
      },
    },
    reducers: {
      save(state, action) {
        
        return {
          ...state,
          ...action.payload,
        };
      },
      // 启用 immer 之后
      // save(state, action) {
      //   state.name = action.payload;
      // },
    },
    subscriptions: {
      setup({ dispatch, history }) {
        return history.listen(({ pathname }) => {
          // if (pathname === '/') {
          //   dispatch({
          //     type: 'query',
          //   })
          // }
        });
      }
    }
  };
  
  export default StudentModel;
  